<div class="row">
  <button type="button" id="start-btn" class="btn btn-primary disabled">启动比对行情</button>
  <button type="button" id="stop-btn" class="btn btn-danger disabled">停止比对行情</button>
  <button type="button" id="download-btn" class="btn btn-danger">下载比对行情</button>
  <button type="button" id="w-btn" class="btn btn-default disabled"></button>
  <button type="button" id="r-btn" class="btn btn-default disabled"></button>
  <p/>
  <div class="panel panel-default">
    <div id="connect-status" class="panel-body"></div>
  </div>
</div>
<div class="row">
  <table class="mitake-table" border="1">
    <thead>
      <tr>
        <th>机房</th>
        <th>响应</th>
        <th>状态</th>
        <th>代码</th>
        <th>名称</th>
        <th>交易时间</th>
        <th>市场别</th>
        <th>次类别</th>
        <th>最新价</th>
        <th>最高价</th>
        <th>最低价</th>
        <th>今开价</th>
        <th>昨收价</th>
        <th>涨跌比率</th>
        <th>总量</th>
        <th>当前成交量</th>
        <th>换手率</th>
        <th>涨停价</th>
        <th>跌停价</th>
        <th>均价</th>
        <th>涨跌</th>
        <th>成交金额</th>
        <th>量比</th>
        <th>买一价</th>
        <th>卖一价</th>
        <th>外盘量</th>
        <th>内盘量</th>
        <th>总值</th>
        <th>流值</th>
        <th>净资产</th>
        <th>PE(市盈)</th>
        <th>ROE(净资产收益率)</th>
        <th>总股本</th>
        <th>流通股</th>
        <th>五档买价</th>
        <th>五档卖价</th>
        <th>五档买量</th>
        <th>五档卖量</th>
        <th>五档买单量</th>
        <th>五档卖单量</th>
        <th>振幅比率</th>
        <th>收益</th>
        <th>交易明细</th>
        <th>上涨家数</th>
        <th>平盘家数</th>
        <th>下跌家数</th>
      </tr>
    </thead>
    <tbody id="list-tbody">
    </tbody>
  </table>
</div>
<script type="text/javascript">
var updateConnectStatus = '';
var pushIndex = -1;

$(document).ready(function()
{
	clearInterval(updateConnectStatus);
	updateConnectStatus = setInterval("getConnectStatus()", 1000);
	
	$('#start-btn').on('click', function() 
	{
		$('#start-btn').addClass('disabled');
		$('#stop-btn').removeClass('disabled');
		startService();
 	});

	$('#stop-btn').on('click', function() 
	{
		$('#start-btn').removeClass('disabled');
		$('#stop-btn').addClass('disabled');
		
		stopService();
 	});
	$('#download-btn').on('click',function(){
		downloadService();
	});
});

function  downloadService()
{
	
	var form=$("<form>");//定义一个form表单
	form.attr("style","display:none");
	form.attr("target","");
	form.attr("method","post");
	form.attr("action","/d?event=download");
	var input1=$("<input>");
	input1.attr("type","hidden");
	input1.attr("name","exportData");
	input1.attr("value",(new Date()).getMilliseconds());
	$("body").append(form);//将表单放置在web中
	form.append(input1);
	form.submit();//表单提交 

}

function isRunning()
{
	send('/d', 'event=t', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		if(info.code == '00000')
		{
			if(info.running == 'true')
			{
				$('#stop-btn').removeClass('disabled');
			}
			else
			{
				$('#start-btn').removeClass('disabled');
			}
		}
		else
		{
			$('#message-body').text(info.message);
			$('#message-dialog').modal('show');
		}
	});
}

function startService()
{
	send('/d', 'event=s', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		//updateConnectStatus = setInterval("getConnectStatus()", 1000);
		
		//$('#message-body').text(info.message);
		//$('#message-dialog').modal('show');
	});
}

function stopService()
{
	send('/d', 'event=e', function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
		$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		var info = JSON.parse(data);
		
		$('#connect-status').html('');
		//$('#message-body').text(info.message);
		//$('#message-dialog').modal('show');
	});
}

function getConnectStatus()
{
	send('/d', 'event=g&src=' + pushIndex, function callbackComplete(jqXHR, textStatus)
	{}, 
	function callbackError(jqXHR, textStatus, errorThrown)
	{
		$('#message-body').text(errorThrown);
	//	$('#message-dialog').modal('show');
	}, 
	function callbackSuccess(data, textStatus, jqXHR)
	{
		//console.log(data);
		var info = JSON.parse(data);
		
		if(info.code == '00000')
		{
			if(info.running == 'true')
			{
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				$('#start-btn').addClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				if(info.host.length > 0)
				{
					var s = '';
					
					for(i = 0 ; i < info.host.length ; i++)
					{
						s += '(' +　info.host[i].ip + '_' + (info.host[1].status == '0' ? "连线中" : "<font style=\"color:#990000;\">断线中</font>") + '),';
					}

					s += '数据比对中.....';
					
					$('#w-btn').text('发送报文数量:' + info.w);
					$('#r-btn').text('回应报文数量:' + info.r);
					$('#connect-status').html(s);
				}
				
				if(info.push.length > 0)	
				{
					
					//var oldHtml = $('#list-tbody').html();
					//$('#list-tbody').html("");
					var qutotr = "";
					for(i = 0 ; i < info.push.length ; i++)
					{
						qutotr = qutotr +'<tr>';
						qutotr = qutotr +'<td>' + info.push[i].host + '</td>';
						qutotr = qutotr +'<td>' + info.push[i].responseTime + '</td>';
						
						qutotr = qutotr +'<td' + (info.push[i].status.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].status.v + '</td>';
						
						qutotr = qutotr +'<td>' + info.push[i].id + '</td>';
						qutotr = qutotr +'<td>' + info.push[i].name + '</td>';
						
						qutotr = qutotr +'<td' + (info.push[i].datetime.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].datetime.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].market.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].market.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].subtype.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].subtype.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].lastPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].lastPrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].highPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].highPrice.v + '</td>';
						
						qutotr = qutotr +'<td' + (info.push[i].lowPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].lowPrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].openPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].openPrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].preClosePrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].preClosePrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].changeRate.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].changeRate.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].volume.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].volume.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].nowVolume.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].nowVolume.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].turnoverRate.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].turnoverRate.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].limitUP.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].limitUP.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].limitDown.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].limitDown.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].averageValue.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].averageValue.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].change.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].change.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].amount.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].amount.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].volumeRatio.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].volumeRatio.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].buyPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].buyPrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sellPrice.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sellPrice.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].buyVolume.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].buyVolume.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sellVolume.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sellVolume.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].totalValue.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].totalValue.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].flowValue.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].flowValue.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].netAsset.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].netAsset.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].pe.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].pe.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].roe.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].roe.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].capitalization.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].capitalization.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].circulatingShares.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].circulatingShares.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].buyPricesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].buyPricesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sellPricesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sellPricesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].buyVolumesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].buyVolumesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sellVolumesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sellVolumesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].buySingleVolumesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].buySingleVolumesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sellSingleVolumesTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sellSingleVolumesTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].amplitudeRate.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].amplitudeRate.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].receipts.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].receipts.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].tradeTickTmp.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].tradeTickTmp.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].upCount.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].upCount.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].sameCount.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].sameCount.v + '</td>';
						qutotr = qutotr +'<td' + (info.push[i].downCount.d == 'Y' ? ' style=\"color: #ff0000;\"' : '') + '>' + info.push[i].downCount.v + '</td>';
						qutotr = qutotr +'</tr>';
						console.log($('#list-tbody').html());
						if($.trim($('#list-tbody').html()) == ''){
							$('#list-tbody').append(qutotr);
						}else{
							$('#list-tbody tr:first').before(qutotr);
						}
						
					}
					
					pushIndex = parseInt(info.index);
				}
			}
			else
			{
				$('#connect-status').html('');
				
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').removeClass('disabled');
				
				$('#start-btn').removeClass('disabled');
				$('#stop-btn').addClass('disabled');
			}
		}
		else
		{
			$('#message-body').text(info.message);
			$('#message-dialog').modal('show');
		}
	});
}
</script>